package com.example.dllo.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.dllo.Activity.Answer_Content_Activity;
import com.example.dllo.interface_.OnMyItemClick;
import com.example.dllo.adapter.AnswerAdapter;
import com.example.dllo.entity.AnswerList;
import com.example.dllo.entity.AnswerResult;
import com.example.dllo.farm_doctor.R;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Answer_Fragment extends Fragment implements OnMyItemClick {

    private RecyclerView recyclerView;
    private AnswerAdapter answerAdapter;
    private RequestQueue queue;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_answer_,null);
        return  view;
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);

        //绑定适配器
        answerAdapter = new AnswerAdapter(getActivity());
        recyclerView.setAdapter(answerAdapter);
        answerAdapter.setOnMyItemClick(this);
        initData();
    }


    private void initData() {
        queue = Volley.newRequestQueue(getActivity());

        String url = "http://www.nongyisheng.com/question/latest2?um=_yingyongbao&fr=android&rn=20&category=0&sys=5.0.2&ver=2.1.0&vc=36";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        Gson gson = new Gson();
                        AnswerResult result = gson.fromJson(s,
                                AnswerResult.class);
                        if (result != null) {
                            List<AnswerList> answerLists = result.getList();
                            if (answerLists != null) {
                                answerAdapter.addData(answerLists);

                            }

                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();

            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;
            }
        };
        queue.add(request);

    }

    @Override
    public void onMyItemClick(AnswerList o) {
        Intent intent = new Intent();
        intent.setClass(getActivity(),Answer_Content_Activity.class);
        intent.putExtra("id",o.getId());
        startActivity(intent);
    }
}
